<?php
/**
 * Created by PhpStorm.
 * User: nirui
 * Date: 17-7-14
 * Time: 上午11:54
 */
class Controller_Newreport_currentstocks extends Controller_Admin{
    public function action_index(){
        set_time_limit(0);
        $this->title='日结库存查询';
        $view = View::factory('newreport/currentstocks');
        if(isset($_GET['checksign'])&&$_GET['checksign']==1){
            $currentstock=new currentstocks();
            if(isset($_GET['download'])&&$_GET['download']==1){
                $filename='disk/'.currentstocks::getFilename().date('YmdHi').'.csv';
                $count=currentstocks::countData($_GET);
                if($count>currentstocks::$maxDownloadNumber){
                    exit('1qw');
                }
                if($count<5000){
                    $infos=currentstocks::getData($_GET);
                    currentstocks::download(array_keys($currentstock->title),$filename
                        ,$currentstock->title,$infos);
                    exit($filename);
                }else{
                    $i=ceil($count/currentstocks::$downloadLimitNumber);
                    $startCount=0;
                    for($c=1;$c<=$i;$c++){
                        $limitsql=" LIMIT ".($startCount).",".currentstocks::$downloadLimitNumber;
                        $infos=currentstocks::getData($_GET,$limitsql);
                        currentstocks::download(array_keys($currentstock->title),$filename
                            ,$currentstock->title,$infos,1);
                        $startCount=$c*Purchaseorderdetails::$downloadLimitNumber;
                    }
                    exit($filename);
                }
            }
            $view->set('title',$currentstock->title);
            $count=currentstocks::countData($_GET);
            $pagination = Pagination::factory(array(
                'items_per_page' => 30,
                'total_items' => $count,
                'uri' => $this->request->uri(),
            ));
            $current_page=isset($_GET['page'])?$_GET['page']:1;
            $start_page=($current_page==0)?0:($current_page-1)*30;
            $view->set('pagination',$pagination);

            $limitsql=" LIMIT ".($start_page).",30";
            $infos=currentstocks::getData($_GET,$limitsql);
            $view->set('infos',$infos);

        }
        $this->response->body($view);
    }

    public function action_create(){
        set_time_limit(0);
        $new=new currentstocks();
        if(isset($_GET['reference_date'])){
            $currentDate=$_GET['reference_date'];
            $date=date('Y-m-00',strtotime($currentDate));
        }else{
            $currentDate=date('Y-m-d');
            $maxDate=DB::query(Database::SELECT,'SELECT MAX(`reference_date`) `maxRD` FROM `daylystockinfos`')
                ->execute('newerp')
                ->get('maxRD');
            $date=is_null($maxDate)?date('Y-m-00',strtotime($currentDate)):$maxDate;
        }
        while(strtotime($date)<strtotime($currentDate)){
            $date=date('Y-m-d',strtotime("$date +1 day"));
            $new->createBaseData($date);
        }exit;
    }
}